package com.stone.business.equipment.service;

/**
 * <pre>
 * 设备Service
 * </pre>
 */

import java.util.List;

import com.stone.business.equipment.pojo.TEquipmentBase;
import com.stone.business.equipment.pojo.TEquipmentParamByq;
import com.stone.business.equipment.pojo.TEquipmentParamByqBtqtjdq;
import com.stone.business.equipment.pojo.TEquipmentParamByqCyg;
import com.stone.business.equipment.pojo.TEquipmentParamByqFj;
import com.stone.business.equipment.pojo.TEquipmentParamByqLqq;
import com.stone.business.equipment.pojo.TEquipmentParamByqQyb;
import com.stone.business.equipment.pojo.TEquipmentParamByqTg;
import com.stone.business.equipment.pojo.TEquipmentParamByqTyzz;
import com.stone.business.equipment.pojo.TEquipmentParamByqYljdq;
import com.stone.business.equipment.pojo.TEquipmentParamByqYlsff;
import com.stone.business.equipment.pojo.TEquipmentParamByqYzjdq;
import com.stone.business.equipment.pojo.TEquipmentParamDlhgq;
import com.stone.business.equipment.pojo.TEquipmentParamDlhgqRz;
import com.stone.business.equipment.pojo.TEquipmentParamDlq;
import com.stone.business.equipment.pojo.TEquipmentParamDyhgq;
import com.stone.business.equipment.pojo.TEquipmentParamDyhgqRz;
import com.stone.business.equipment.pojo.TEquipmentParamGlkg;
import com.stone.business.equipment.pojo.TEquipmentParamKgg;
import com.stone.business.equipment.pojo.TEquipmentParamZhdq;

import frm.ssh.exception.AppException;
import frm.ssh.service.BaseService;

public class EquipmentCommonService extends BaseService {
	
	/**
	 * 保存
	 * @param ids
	 * @throws AppException
	 */
	public String saveEquip(List<Object> list) throws  AppException{
		
		String paraid = "";
		
		TEquipmentBase entity = (TEquipmentBase) list.get(0);

		// 新建
		if (entity.getId() == 0) {

			super.save(entity);

			// 开关柜
			if ("005".equals(entity.getSblx())) {	
				TEquipmentParamKgg paramKgg = (TEquipmentParamKgg) list.get(1);
				paramKgg.setBaseid(entity.getId());
				super.save(paramKgg);
				paraid = paramKgg.getId().toString();
				
				// 电流互感器
			} else  if ("006".equals(entity.getSblx())) {
				TEquipmentParamDlhgq paramDlhgq = (TEquipmentParamDlhgq) list.get(1);
				paramDlhgq.setBaseid(entity.getId());
				super.save(paramDlhgq);
				paraid = paramDlhgq.getId().toString();
					
				// 电压互感器
			} else  if ("007".equals(entity.getSblx())) {
				TEquipmentParamDyhgq paramDyhgq = (TEquipmentParamDyhgq) list.get(1);
				paramDyhgq.setBaseid(entity.getId());
				super.save(paramDyhgq);
				paraid = paramDyhgq.getId().toString();
					
				// 组合电器
			} else  if ("004".equals(entity.getSblx())) {
				TEquipmentParamZhdq paramZhdq = (TEquipmentParamZhdq) list.get(1);
				paramZhdq.setBaseid(entity.getId());
				super.save(paramZhdq);
				paraid = paramZhdq.getId().toString();
				
				// 隔离开关
			} else  if ("003".equals(entity.getSblx())) {
				TEquipmentParamGlkg paramGlkg = (TEquipmentParamGlkg) list.get(1);
				paramGlkg.setBaseid(entity.getId());
				super.save(paramGlkg);
				paraid = paramGlkg.getId().toString();
				
				// 断路器
			} else  if ("002".equals(entity.getSblx())) {
				TEquipmentParamDlq paramDlq = (TEquipmentParamDlq) list.get(1);
				paramDlq.setBaseid(entity.getId());
				super.save(paramDlq);
				paraid = paramDlq.getId().toString();
				
				// 变压器
			} else  if ("001".equals(entity.getSblx())) {
				TEquipmentParamByq paramByq = (TEquipmentParamByq) list.get(1);
				paramByq.setBaseid(entity.getId());
				super.save(paramByq);
				paraid = paramByq.getId().toString();
				
				// 二次设备
			} else  if (entity.getSblx().startsWith("E")) {
				paraid = "E";
			} 

			// 修改
		} else {
			
			if (entity.getModelid() <= 0) {

				super.update(entity);
				
				// 开关柜
				if ("005".equals(entity.getSblx())) {
					TEquipmentParamKgg paramKgg = (TEquipmentParamKgg) list.get(1);
					super.update(paramKgg);
					paraid = paramKgg.getId().toString();
					
					// 电流互感器
				} else if ("006".equals(entity.getSblx())) {
					TEquipmentParamDlhgq paramDlhgq = (TEquipmentParamDlhgq) list.get(1);
					super.update(paramDlhgq);
					paraid = paramDlhgq.getId().toString();
					
					// 电压互感器
				} else if ("007".equals(entity.getSblx())) {
					TEquipmentParamDyhgq paramDyhgq = (TEquipmentParamDyhgq) list.get(1);
					super.update(paramDyhgq);
					paraid = paramDyhgq.getId().toString();
					
					// 组合电器
				} else if ("004".equals(entity.getSblx())) {
					TEquipmentParamZhdq paramZhdq = (TEquipmentParamZhdq) list.get(1);
					super.update(paramZhdq);
					paraid = paramZhdq.getId().toString();
					
					// 隔离开关
				} else if ("003".equals(entity.getSblx())) {
					TEquipmentParamGlkg paramGlkg = (TEquipmentParamGlkg) list.get(1);
					super.update(paramGlkg);
					paraid = paramGlkg.getId().toString();
					
					// 断路器
				} else if ("002".equals(entity.getSblx())) {
					TEquipmentParamDlq paramDlq = (TEquipmentParamDlq) list.get(1);
					super.update(paramDlq);
					paraid = paramDlq.getId().toString();
					
					// 变压器
				} else if ("001".equals(entity.getSblx())) {
					TEquipmentParamByq paramByq = (TEquipmentParamByq) list.get(1);
					paramByq.setBaseid(entity.getId());
					super.update(paramByq);
					paraid = paramByq.getId().toString();
					
					// 二次设备
				} else  if (entity.getSblx().startsWith("E")) {
					paraid = "E";
				} 
				
				
				// 参照新建
			} else {
				
				entity.setModelid(-entity.getModelid());
				super.save(entity);

				// 开关柜
				if ("005".equals(entity.getSblx())) {	
					TEquipmentParamKgg paramKgg = (TEquipmentParamKgg) list.get(1);
					paramKgg.setBaseid(entity.getId());
					super.save(paramKgg);
					paraid = paramKgg.getId().toString();
					
					// 电流互感器
				} else  if ("006".equals(entity.getSblx())) {
					TEquipmentParamDlhgq paramDlhgq = (TEquipmentParamDlhgq) list.get(1);
					paramDlhgq.setBaseid(entity.getId());
					super.save(paramDlhgq);
					paraid = paramDlhgq.getId().toString();
						
					// 电压互感器
				} else  if ("007".equals(entity.getSblx())) {
					TEquipmentParamDyhgq paramDyhgq = (TEquipmentParamDyhgq) list.get(1);
					paramDyhgq.setBaseid(entity.getId());
					super.save(paramDyhgq);
					paraid = paramDyhgq.getId().toString();
						
					// 组合电器
				} else  if ("004".equals(entity.getSblx())) {
					TEquipmentParamZhdq paramZhdq = (TEquipmentParamZhdq) list.get(1);
					paramZhdq.setBaseid(entity.getId());
					super.save(paramZhdq);
					paraid = paramZhdq.getId().toString();
					
					// 隔离开关
				} else  if ("003".equals(entity.getSblx())) {
					TEquipmentParamGlkg paramGlkg = (TEquipmentParamGlkg) list.get(1);
					paramGlkg.setBaseid(entity.getId());
					super.save(paramGlkg);
					paraid = paramGlkg.getId().toString();
					
					// 断路器
				} else  if ("002".equals(entity.getSblx())) {
					TEquipmentParamDlq paramDlq = (TEquipmentParamDlq) list.get(1);
					paramDlq.setBaseid(entity.getId());
					super.save(paramDlq);
					paraid = paramDlq.getId().toString();
					
					// 变压器
				} else  if ("001".equals(entity.getSblx())) {
					TEquipmentParamByq paramByq = (TEquipmentParamByq) list.get(1);
					paramByq.setBaseid(entity.getId());
					super.save(paramByq);
					paraid = paramByq.getId().toString();
					
					// 二次设备
				} else  if (entity.getSblx().startsWith("E")) {
					paraid = "E";
				} 
			}
			
		}
		
		return paraid;

	}

	/**
	 * 删除
	 * @param ids
	 * @throws AppException
	 */
	public void deleteEquip(String ids) throws  AppException{
		
		// 查询出设备
		List<TEquipmentBase> rtn = super.findListByHql(" from TEquipmentBase where id in (" + ids + ")");
		
		// 根据设备类型删除子表信息
		for (TEquipmentBase e : rtn) {
			
			// 变压器
			if ("001".equals(e.getSblx())) {
				
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByq.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqFj.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqLqq.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqCyg.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqYlsff.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqYzjdq.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqBtqtjdq.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqQyb.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqYljdq.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqTg.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamByqTyzz.class.getName());
				
				// 断路器
			} else if ("002".equals(e.getSblx())) {
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamDlq.class.getName());
				
				// 隔离开关
			} else if ("003".equals(e.getSblx())) {
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamGlkg.class.getName());
				
				// 组合电器
			} else if ("004".equals(e.getSblx())) {
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamZhdq.class.getName());
				
				// 开关柜
			} else if ("005".equals(e.getSblx())) {
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamKgg.class.getName());
				
				// 电流互感器
			} else if ("006".equals(e.getSblx())) {
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamDlhgq.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamDlhgqRz.class.getName());
				
				// 电压互感器
			} else if ("007".equals(e.getSblx())) {
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamDyhgq.class.getName());
				super.deleteByDHql("baseid in (" + ids + ")", null, TEquipmentParamDyhgqRz.class.getName());
			}
			
			// 删除主表
			super.deleteAll(ids, TEquipmentBase.class.getName());

		}

	}
}
